package cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.dataobject.goods;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.controller.admin.goods.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserImportExcelVO;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.*;

import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.annotation.*;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;

/**
 * 小程序-商品 DO
 *
 * @author 芋道源码
 */
@TableName(value = "qd_goods",autoResultMap = true)
@KeySequence("qd_goods_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库，可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@AllArgsConstructor
public class GoodsDO extends BaseDO {

    /**
     * id主键
     */
    @TableId
    private Integer id;
    /**
     * 商品名称
     */
    private String name;
    /**
     * 商品编号
     */
    private String code;
    /**
     * 图片
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<String> img;
    /**
     * 商品分类
     */
//    @TableField(typeHandler = FastjsonTypeHandler.class)
    private String type;
    /**
     * 销售数量
     */
    private Integer saleNum;
    /**
     * 运费模版id
     */
    private Integer postalId;
    /**
     * 运费模版名称
     */
    private String postalName;
    /**
     * 是否包邮(1是2否)
     */
    private Integer postage;
    /**
     * 状态1上架2下架
     */
    private Integer status;
    /**
     * 商品描述
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<GoodContentVo> content;
    /**
     * 图文详情
     */
    private String imgInfo;
    /**
     * 规格
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<GoodSpecif> specif;
    /**
     * 折扣规格
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<GoodSpecif> disSpecif;
    /**
     * 折扣id
     */
    private Integer disId;
    /**
     * 是否是折扣商品 1是2否
     */
    private Integer isDisGoods;
    //商品最低价格
    private BigDecimal minPrice;
    //供应商id
    private Integer supplierId;
    //供应商名称
    private String supplierName;
    //条形码
    private String barCode;
    //标价
    private BigDecimal listPrice;
    //折扣价格(售价)
    private BigDecimal disPrice;

    //商品属性
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<GoodsAttr> attributes;


    //普通商品名称
    @TableField(exist = false)
    private String oneName;
    //普通商品图片
    @TableField(exist = false)
    private String oneImg;
    //普通商品价格
    @TableField(exist = false)
    private BigDecimal onePrice;
    //普通商品库存
    @TableField(exist = false)
    private Integer oneSize;

    //分页商品价格
    @TableField(exist = false)
    private BigDecimal goodsPrice;
    //分页商品库存
    @TableField(exist = false)
    private Integer goodsSize;

    public GoodsDO() {
    }

    //批量导入
    public GoodsDO(GooodImportExcelVO vo) {
        this.name = vo.getName();
        this.code = vo.getCode();
        String imgs = vo.getImgs()==null?"":vo.getImgs();
        this.img= new ArrayList<>(Arrays.asList(imgs.split("&")));
        this.type="13,42,43";
        this.postage = 1;
        int status=1;
        if (!vo.getStatus().equals("上架"))
            status=2;
        this.status = status;

        List<GoodSpecif> specif=new ArrayList<>();
        GoodSpecif goodSpecif = new GoodSpecif();
        goodSpecif.setId(UUID.randomUUID().toString());
        goodSpecif.setName("默认");
        goodSpecif.setUrl("https://jockers.oss-cn-beijing.aliyuncs.com/20230520/766569b7fd864ad8b201e443a69a0208.jpg");
        List<GoodSpecifChild> child=new ArrayList<>();
        GoodSpecifChild specifChild = new GoodSpecifChild();
        specifChild.setId(UUID.randomUUID().toString());
        specifChild.setSize("默认");
        specifChild.setPrice(new BigDecimal(vo.getPrice()));
        specifChild.setNum(Integer.valueOf(vo.getNum()));
        child.add(specifChild);
        goodSpecif.setChild(child);
        specif.add(goodSpecif);
        this.specif=specif;

        try {
            String skuCode = vo.getSkuCode();
            List<GoodsAttr> list=new ArrayList<>();
            for (String row : skuCode.split("&")) {
                String[] split = row.split("-");
                list.add(new GoodsAttr(split[0],split[1]));
            }
            this.attributes=list;
        }catch (Exception e){
        }

    }
}
